package com.alex.statistics.method.timeSeriesAnalysis;


import org.springframework.stereotype.Service;

@Service
public class MovingAverageService {

    /**
     * 计算给定数据的移动平均
     *
     * @param data 输入数据
     * @param windowSize 移动窗口大小
     * @return 预测结果
     */
    public double predict(double[] data, int windowSize) {
        if (data.length < windowSize) {
            throw new IllegalArgumentException("数据长度小于窗口大小");
        }

        // 计算移动平均
        double sum = 0;
        for (int i = data.length - windowSize; i < data.length; i++) {
            sum += data[i];
        }

        return sum / windowSize;
    }
}
